package com.jf.identity.service.impl;

import com.jf.identity.entity.ExamBaseParam;
import com.jf.identity.entity.ExamBaseParamValidator;
import com.jf.identity.entity.VerifyAbnormalInfo;
import com.jf.identity.entity.VerifyInfoFeedBack;
import com.jf.identity.exception.ServiceException;
import com.jf.identity.holder.ExamBaseParamHolder;
import com.jf.identity.mongo.VerifyInfoDao;
import com.jf.identity.service.VerifyInfoService;
import com.jf.identity.util.PostPictureUtil;
import com.jf.identity.util.UUIDUtil;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author FCG
 */
@Service
public class VerifyInfoServiceImpl implements VerifyInfoService {
    @Resource
    private VerifyInfoDao verifyInfoDao;
    @Resource
    private ExamBaseParamHolder examBaseParamHolder;
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private ExamBaseParamValidator examBaseParamValidator;
//    @Resource
//    OrgCodeCatchUtil orgCodeCatchUtil;

    @Override
    public boolean insertVerifyInfo(VerifyInfoFeedBack verifyInfoFeedBack) {
        ExamBaseParam examBaseParam = examBaseParamHolder.getExamBaseParam();
        ExamBaseParam.isBlank(examBaseParam);
        examBaseParamValidator.validate(examBaseParam,ExamBaseParam.EXAM_ID, ExamBaseParam.EXAM_SESSION_NUM,
                ExamBaseParam.ORGCODE);

        try{
            //
            int i = Integer.parseInt(examBaseParam.getExamSessionNum());
            if (i <0){
                throw new ServiceException("ExamSessionNum必须大于0");
            }
        }catch(Exception exception){
            throw new IllegalArgumentException("ExamSessionNum必须大于0");
        }

        //设置上报时间
        verifyInfoFeedBack.setDateTime(new Date());
        //存到monogoDB
        verifyInfoFeedBack.setTimestamp(new Date());
        mongoTemplate.insert(verifyInfoFeedBack);
        return true;
    }

    @Override
    public boolean insertVerifyAbnormalInfo(List<VerifyAbnormalInfo> verifyAbnormalInfos) {
        ExamBaseParam examBaseParam = examBaseParamHolder.getExamBaseParam();
        ExamBaseParam.isBlank(examBaseParam);
        examBaseParamValidator.validate(examBaseParam,ExamBaseParam.EXAM_ID, ExamBaseParam.EXAM_SESSION_NUM,
                ExamBaseParam.ORGCODE);

        try{
            //
            int i = Integer.parseInt(examBaseParam.getExamSessionNum());
            if (i <0){
                throw new ServiceException("ExamSessionNum必须大于0");
            }
        }catch(Exception exception){
            throw new IllegalArgumentException("ExamSessionNum必须大于0");
        }
//        Map<String,String> orgCodeMap = orgCodeCatchUtil.getOrgCodeCatch();22
        for (VerifyAbnormalInfo verifyAbnormalInfo : verifyAbnormalInfos) {
            if (verifyAbnormalInfo == null) {
                continue;
            }
            verifyAbnormalInfo.setUid(UUIDUtil.generateUUID());
            verifyAbnormalInfo.setExamBaseParam(examBaseParam);
            String type = null;
            if("1".equals(verifyAbnormalInfo.getTZLX())){
                type = "5";
            }else if ("2".equals(verifyAbnormalInfo.getTZLX())){
                type = "4";
            }
            //todo:图片不存入MongoDB中，先将图片存入本地，然后将YZTP替换成路径
            if(type != null && !StringUtils.isEmpty(verifyAbnormalInfo.getYZTP())){
                String picURL  = PostPictureUtil.postPicAtLocal(verifyAbnormalInfo.getYZTP(),type,examBaseParam.getExamId());
                verifyAbnormalInfo.setYZTP(picURL);
            }
            verifyAbnormalInfo.setTimestamp(new Date());
            verifyInfoDao.insertVerfifyInfo(verifyAbnormalInfo);
        }
        return true;
    }
}
